package pub.tbc.atps.collector.receiver.redis;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.atps.D;
import pub.tbc.atps.collector.receiver.Receiver;
import pub.tbc.atps.config.PropConfig;
import pub.tbc.atps.task.RestTask;
import pub.tbc.atps.util.RedisHelper;
import pub.tbc.toolkit.Objs;
import redis.clients.jedis.Jedis;

/**
 * @author tbc on 2016/11/13 2:14.
 */
@Slf4j
public class RedisReceiver implements Receiver {
    private String keys = PropConfig.get("task.queue.key");

    public RestTask getTask() {
        String listener = listener();
        RestTask restTask = null;
        try {
            restTask = JSON.parseObject(listener, RestTask.class);
        } catch (Exception e) {
            String message = "->" + e.getMessage();
            log.error(message);
            throw new RuntimeException(message, e);
        }
        return restTask;
    }

    public String listener() {
        Jedis jedis = RedisHelper.getClientFromPool();
        String message = jedis.lpop(keys);
//        if (Objs.isEmpty(message)) {
//            D.endTime = System.currentTimeMillis();
//            System.out.println("消费全部消息，共耗时： " + (D.endTime - D.startTime));
////            System.exit(0);
//        }
        jedis.close();
        return message;
    }
}
